#!/bin/sh -ef
#
# Copyright (C) 2003-2019  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2007  Kirill A. Shutemov <kas@altlinux.org>
# 
# The hsh-mkchroot utility for the hasher project
#
# SPDX-License-Identifier: GPL-2.0-or-later
#

. hsh-sh-functions

show_help()
{
	cat <<EOF
hsh-mkchroot - create initial chroot.

Usage: $PROG [options] [<path-to-workdir>]

<path-to-workdir> must be valid writable directory.

Options:
  --hasher-priv-dir=DIR     hasher-priv directory;
  --number=NUMBER           subconfig identifier;
  --wait-lock               wait for workdir and hasher-priv locks;
  --no-wait-lock            do not wait for workdir and hasher-priv locks;
  --with-qemu=ARCH          copy qemu executable into the chroot .host directory;
  -q, --quiet               try to be more quiet;
  -v, --verbose             print a message for each action;
  -V, --version             print program version and exit;
  -h, --help                show this text and exit.
    
Report bugs to http://bugs.altlinux.ru/

EOF
	exit
}

TEMP=`getopt -n $PROG -o $getopt_common_opts -l hasher-priv-dir:,no-lock,number:,wait-lock,no-wait-lock,with-qemu:,$getopt_common_longopts -- "$@"` ||
	show_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--hasher-priv-dir)
			hasher_priv_dir="$(opt_check_dir "$1" "$2")"
			shift
			;;
		--no-lock) no_lock=1
			;;
		--number)
			if [ -z "${2##/*}" ]; then
				[ -d "$2" ] ||
					fatal "$1: $2: Directory not available."
				number="$2"
			else
				number="$(opt_check_number_ge_0 "$1" "$2")"
			fi
			shift
			;;
		--wait-lock) lock_nowait=
			;;
		--no-wait-lock) lock_nowait=1
			;;
		--with-qemu)
			qemu_arch="$2"
			shift
			;;
		--) shift; break
			;;
		*) parse_common_option "$1"
			;;
	esac
	shift
done

if [ -z "$workdir" ]; then
	# Exactly one argument.
	[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'
	[ "$#" -le 1 ] || show_usage 'Too many arguments.'
else
	# At most one argument.
	[ "$#" -le 1 ] || show_usage 'Too many arguments.'
fi

if [ "$#" -ge 1 ]; then
	set_workdir "$1"
	shift
else
	set_workdir
fi

lock_workdir

# exists already?
[ ! -e chroot -a ! -L chroot ] ||
	fatal "Remove $chroot first."

if [ -n "$number" -a -z "${number##/*}" ]; then
	deduce_lock_hasher_priv "$number"
else
	allocate_lock_hasher_priv
fi

ugid1="$("$getugid1" ${number:+-$number})" ||
	fatal "getugid1 failed."
gid1="$(printf %s "$ugid1" |cut -d: -f2)"
ugid2="$("$getugid2" ${number:+-$number})" ||
	fatal "getugid2 failed."
gid2="$(printf %s "$ugid2" |cut -d: -f2)"

mkdir -m1775 $verbose -- chroot >&2
cd chroot
verbose "Changed working directory to \`$chroot'."

mkdir -m711 $verbose .host >&2
mkdir -m755 $verbose .in dev >&2
chgrp "$gid1" $verbose . dev >&2
mkdir -m1770 $verbose .out >&2
chgrp "$gid2" $verbose .out >&2
verbose 'Created directory tree.'

if [ -x /usr/libexec/hasher/busybox ]; then
	install -p -m755 $verbose /usr/libexec/hasher/busybox .host/ >&2
	./.host/busybox --list |
		xargs -I{} ln -s $verbose busybox .host/{} >&2
else
	install_static_helper ash sh
	install_static_helper find
	install_static_helper cpio
fi
[ -z "$qemu_arch" ] ||
	install_static_helper "qemu-$qemu_arch" "qemu-${qemu_arch/armh/arm}"
postin="$HOME/.hasher/install/post"
if [ -r "$postin" -a -x "$postin" ]; then
	install -p -m755 $verbose "$postin" .host/postin >&2
fi
verbose 'Installed .host programs.'
